package com.prabhakar.taxiclubapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

public class P2PActivity extends Activity {

	private static String Tag ="P2PActivity ";
	Spinner spinner;

	// Progress Dialog
	private ProgressDialog pDialog;

	JSONParser jsonParser = new JSONParser();
	String name = "";
	String phoneNumber = "";
	String pickUpLocation = "";
	String destination = "";
	String email = "";
	String carType = "";
	String pickUpDate = "";
	String pickupTime = "";
	String noOfPassengers = "";

	Calendar myCalendar = Calendar.getInstance();
	public static final String myFormat = "MM/dd/yy";
	DatePickerDialog.OnDateSetListener date;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.p2p_main);

		/*
		 * this part of code is responsible for showing a calendar in popup ..
		 * fetching date ,formatting it & storing pack in the editTex
		 */
		final DatePickerDialog.OnDateSetListener date;	
		final Button selectDate  = (Button)findViewById(R.id.datepicker);
		final Calendar myCalendar = Calendar.getInstance();


		date = new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				myCalendar.set(Calendar.YEAR, year);
				myCalendar.set(Calendar.MONTH, monthOfYear);
				myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
				updateLabel();
			}

			private void updateLabel(){
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
				//	edittext.setText(sdf.format(myCalendar.getTime()));
				pickUpDate = sdf.format(myCalendar.getTime());	// edittext.getText().toString();
			}

		};

		selectDate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new DatePickerDialog(P2PActivity.this, date, myCalendar
						.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
						myCalendar.get(Calendar.DAY_OF_MONTH)).show();
			}
		});			// PickupDate Popup Code ends.....


		/*  
		 * this part of code is responsible for showing a TIMEPICKER in popup ..
		 * fetching TiME ,formatting it & storing pack in the editText
		*/
		final Button selectTime  = (Button)findViewById(R.id.timePicker);
		selectTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Calendar mcurrentTime = Calendar.getInstance();
				int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
				int minute = mcurrentTime.get(Calendar.MINUTE);
				int AM_PM = mcurrentTime.get(Calendar.AM_PM);
				Log.v(Tag,"radioButtonId > "+AM_PM +"AM_PM > "+AM_PM);

				TimePickerDialog mTimePicker;
				mTimePicker = new TimePickerDialog(P2PActivity.this, new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
						//selectTime.setText( selectedHour + ":" + selectedMinute);
						pickupTime =  ""+selectedHour+"."+selectedMinute; //editTextTime.getText().toString();
					}
				}, hour, minute, false);//No 24 hour time -hence, false..!!!
				mTimePicker.setTitle("Select Time");
				mTimePicker.show();

			}
		});					// PickupTime Popup Code ends here.....

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.p2, menu);
		return true;
	}

	public void formSubmit(View v){

		boolean inValidFormData = false;

		final EditText editText1 = (EditText)findViewById(R.id.name);
		if(editText1.getText().length()==0){
			inValidFormData = true;
			editText1.setError(Constants.NAME_REQUIRED);
		}else{
			name = editText1.getText().toString();
		}

		final EditText editText2 = (EditText)findViewById(R.id.phoneNumber);
		if(editText2.getText().length()==0){
			inValidFormData = true;
			editText2.setError(Constants.PHONENUMBER_REQUIRED);
		}else if(editText2.getText().length()<10 || editText2.getText().length()>10){
			inValidFormData = true;
			editText2.setError(Constants.VALID_PHONENUMBER_REQUIRED);
		}else{
			phoneNumber = editText2.getText().toString();
		}

		final EditText editText3 = (EditText)findViewById(R.id.pickup);
		if(editText3.getText().length()==0){
			inValidFormData = true;
			editText3.setError(Constants.INVALID_PICKUP_LOCATION_MESSAGE);
		}else{
			pickUpLocation = editText3.getText().toString();
		}

		final EditText editText4 = (EditText)findViewById(R.id.destination);
		if(editText4.getText().length()==0){
			inValidFormData = true;
			editText4.setError(Constants.INVALID_DROP_LOCATION_MESSAGE);
		}else{
			destination = editText4.getText().toString();
		}

		final EditText editText5 = (EditText)findViewById(R.id.email);
		if(editText5.getText().length()==0){
			inValidFormData = true;
			editText5.setError(Constants.EMAIL_REQUIRED);
		}else if(!android.util.Patterns.EMAIL_ADDRESS.matcher(editText5.getText()).matches()){
			inValidFormData = true;
			editText5.setError(Constants.VALID_EMAIL_REQUIRED);
		}else{
			email = editText5.getText().toString();
		}

		RadioGroup radioGrp = (RadioGroup)findViewById(R.id.carType);
		int radioButtonId = radioGrp.getCheckedRadioButtonId();
		if(radioButtonId== R.id.taxi_economy) carType = Constants.TAXI_SELECTED_ECONOMY ;
		if(radioButtonId == R.id.taxi_sedan) carType =	Constants.TAXI_SELECTED_SEDAN ;

		spinner = (Spinner)findViewById(R.id.spinner);
		noOfPassengers = String.valueOf(spinner.getSelectedItem());

		Log.v(Tag,"name > "+name +"phoneNumber > "+phoneNumber +"email > "+email+"pickUpLocation > "+pickUpLocation +"destination > "+destination);
		Log.v(Tag,"pickUpDate > "+pickUpDate +"pickupTime > "+pickupTime +"noOfPassengers > "+noOfPassengers);
		Log.v(Tag,"radioButtonId > "+radioButtonId +"carType > "+carType);

		//ASYNC REST-CLIENT call..._ACCN TO PHPSCRIPT
		if(!inValidFormData){		

			//redirect to BookingSummary activity via an Intent to show Map..
			JSONObject json =  new JSONObject();
			try {
				json.put(Constants.NAME, name);
				json.put(Constants.PHONENUMBER, phoneNumber);
				json.put(Constants.EMAIL, email);
				json.put(Constants.PICKUP_LOCATION, pickUpLocation);
				json.put(Constants.DROP_LOCATION, destination);
				json.put(Constants.PICKUP_DATE, pickUpDate);
				json.put(Constants.PICKUP_TIME, pickupTime);
				json.put(Constants.NO_OF_PASSENGERS, noOfPassengers);
				json.put(Constants.CARTYPE, carType);

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Bundle b = new Bundle();
			b.putString("bookingDetails", json.toString());

			Intent bookingSummaryIntent = new Intent();
			bookingSummaryIntent.setComponent(new ComponentName("com.prabhakar.taxiclubapp", "com.prabhakar.taxiclubapp.BookingSummaryActivity"));
			bookingSummaryIntent.putExtras(b);
			P2PActivity.this.startActivity(bookingSummaryIntent);

		}else{
			//some of the form field contains invalid data...
			Toast.makeText(P2PActivity.this,"Unable to proceed , REQUIRED Fields may be Missing or may contain Inappropriate Data.  ", Toast.LENGTH_LONG).show();
		}

	}

}
